<?php
class cls_control_product extends cls_control_base
{
	
	function get_product_list()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		global $smarty;
		$this->smarty->display('product/product_list.htm');
	}
	
	/**
	 * get stockout(short of products) list
	 */
	function get_stockout_list()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		global $smarty;
		$this->smarty->display('product/stockout_list.htm');
	}
	
	/**
	 * return data to the flexgrid
	 */
	function product_list_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
		
		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$where = "WHERE ".$_POST['qtype']." LIKE '%".$_POST['query']."%' ";
		} else {
			$where ='';
		}
		if($_POST['letter_pressed']!=''){
			$where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' ";
		}
		if($_POST['letter_pressed']=='#'){
			$where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' ";
		}
		$sort = "ORDER BY $sortname $sortorder";
		
		
		$product_serve = parent::get_serve();
		$product_list  = $product_serve->get_product_list($page,$rp,$where,$sort);
		$total = $product_serve->getCount($where);

		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;
		$i = 0;
		while($i<count($product_list) )
		{
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "id:'".$product_list[$i]['id']."',";
			$json .= "cell:['".$product_list[$i]['id']."','".$product_list[$i]['Name']."'";
			$json .= ",'".addslashes($product_list[$i]['Publisher'])."'";
			$json .= ",'".addslashes($product_list[$i]['Quantity'])."'";
			$json .= ",'".addslashes($product_list[$i]['Price'])."'";
			
			$json .= ",'".addslashes($product_list[$i]['Category'])."'";
			$json .= ",'".addslashes($product_list[$i]['ISBN'])."']";
				
			$json .= "}";
			$rc = true;
			$i++;
		}
		$json .= "]\n";
		$json .= "}";
		echo $json;
	}
	function search()
	{
	//	$page = $_POST['page'];
	//	$rp = $_POST['rp'];
	//	$sortname = $_POST['sortname'];
	//	$sortorder = $_POST['sortorder'];
		
		//if (!$sortname)
			$sortname = 'a.ID';
		//if (!$sortorder)
			$sortorder = 'asc';
		$qtype = $_REQUEST['qtype'];
		if ($qtype != 'a.Name' && $qtype != 'a.Author' && $qtype != 'a.Publisher')
			$qtype = 'a.Name';
		$query = mysql_escape_string($_REQUEST['query']);
		if($_REQUEST['query']!=''){
			$where = "WHERE ".$qtype." LIKE '%".$query."%' ";
		} else {
			$where ='';
		}
		if($_REQUEST['letter_pressed']!=''){
			$where = "WHERE `".$_REQUEST['qtype']."` LIKE '".$_REQUEST['letter_pressed']."%' ";
		}
		if($_REQUEST['letter_pressed']=='#'){
			$where = "WHERE `".$_REQUEST['qtype']."` REGEXP '[[:digit:]]' ";
		}
		$sort = "ORDER BY $sortname $sortorder";
		
		$product_serve = parent::get_serve();
		$product_list  = $product_serve->get_product_list(1,10000,$where,$sort);
		echo json_encode($product_list);
	}
	/**
	 * get stockout product by appending sql with 鈥渨here quantity鈥�constraint
	 */
	function stockout_list_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
	
		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$where = "WHERE ".$_POST['qtype']." LIKE '%".$_POST['query']."%' ";
		} else {
			$stockout_num = cls_config_product::$stockout_num;
			$where ="where quantity<={$stockout_num}";
		}
		if($_POST['letter_pressed']!=''){
			$where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' "+"and quantity<20";
		}
		if($_POST['letter_pressed']=='#'){
			$where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' "+"and quantity<20";
		}
		$sort = "ORDER BY $sortname $sortorder";
	
		$product_serve = parent::get_serve();
		$product_list  = $product_serve->get_product_list($page,$rp,$where,$sort);
		$total = $product_serve->getCount($where);
		
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;
		$i = 0;
		while($i<count($product_list) )
		{
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "id:'".$product_list[$i]['id']."',";
			$json .= "cell:['".$product_list[$i]['id']."','".$product_list[$i]['Name']."'";
			$json .= ",'".addslashes($product_list[$i]['Publisher'])."'";
			$json .= ",'".addslashes($product_list[$i]['Quantity'])."'";
			$json .= ",'".addslashes($product_list[$i]['Price'])."'";
				
			$json .= ",'".addslashes($product_list[$i]['Category'])."'";
			$json .= ",'".addslashes($product_list[$i]['ISBN'])."']";
		
			$json .= "}";
			$rc = true;
			$i++;
		}
		$json .= "]\n";
		$json .= "}";
		echo $json;
		
	}
	
	function modify_quantity()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_QUANTITY');
		$items = rtrim($_POST['items'],",");
		$product_serve = parent::get_serve();
		$product_serve->modify_quantity($items);
		
		
		echo json_encode(null);
		
		
	}
	
	/**
	 * add product
	 *
	 */
	function add_product()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO');
		
		$name = $_POST['name'];
		$isbn = $_POST['isbn'];
		$publisher = $_POST['publisher'];
		$totalpage = $_POST['totalpage'];
		$description = $_POST['description'];
		$author = $_POST['author'];
		$prolang = $_POST['prolang'];
		$quantity = $_POST['quantity'];
		$price = $_POST['price'];
		$category = $_POST['category'];
	    $path = null;
		if ($_FILES["file"]["error"] > 0)
		{
			echo "Error: " . $_FILES["file"]["error"] . "<br />";
		}
		else
		{
			
			move_uploaded_file($_FILES["file"]["tmp_name"],
			cls_config_product::$save_path . $_FILES["file"]["name"]);
			
			$path = cls_config_product::$read_path . $_FILES["file"]["name"];
		}
		
		$product_serve = parent::get_serve();
		$product_serve ->add_product($name,$publisher,$description,$author,$prolang,$quantity,$price,$category,$path,$totalpage,$isbn);
	    redirection('product.get_product_list');
	}
	
	
	function product_delete()
	{
		
		$items = rtrim($_POST['items'],",");
		$product_serve = parent::get_serve();
		$product_serve->delete_product($items);
		
		echo json_encode(null);
		
	}
	
	function export_product()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		$excel = new PHPExcel();
		$product_serve = parent::get_serve();
		$product_list  = $product_serve->get_product_list(1,1000000,'','');
		$excel->setActiveSheetIndex(0);
		$objActSheet = $excel->getActiveSheet();
		$objActSheet->setTitle('商品列表');
		$objActSheet->setCellValueExplicit('A1', '商品ID', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('B1', '书名', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('C1', '出版商', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('D1', '存货', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('E1', '定价', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('F1', '类别', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('G1', 'ISBN', PHPExcel_Cell_DataType::TYPE_STRING);
		$row = 1;
		foreach ($product_list as $product)
		{
			$row++;
			$col = 65;
			foreach ($product as $item)
			{
				$c = chr($col);
				$objActSheet->setCellValueExplicit("{$c}{$row}", $item, PHPExcel_Cell_DataType::TYPE_STRING);
				$col++;
			}
		}
		export_excel('商品列表', $excel);
	}
	
	function export_stockout_product()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('PRODUCT_INFO', 'PRODUCT_QUANTITY');
		$excel = new PHPExcel();
		$product_serve = parent::get_serve();
		$product_list  = $product_serve->get_product_list(1,1000000,'where quantity<=20','');
		$excel->setActiveSheetIndex(0);
		$objActSheet = $excel->getActiveSheet();
		$objActSheet->setTitle('缺货商品列表');
		$objActSheet->setCellValueExplicit('A1', '商品ID', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('B1', '书名', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('C1', '出版商', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('D1', '存货', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('E1', '定价', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('F1', '类别', PHPExcel_Cell_DataType::TYPE_STRING);
		$objActSheet->setCellValueExplicit('G1', 'ISBN', PHPExcel_Cell_DataType::TYPE_STRING);
		$row = 1;
		foreach ($product_list as $product)
		{
			$row++;
			$col = 65;
			foreach ($product as $item)
			{
				$c = chr($col);
				$objActSheet->setCellValueExplicit("{$c}{$row}", $item, PHPExcel_Cell_DataType::TYPE_STRING);
				$col++;
			}
		}
		export_excel('缺货商品列表', $excel);
	}
}